package handler

import (
	"article-sys/cache"
	"article-sys/model"
	"article-sys/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func ArticleUserList(c *gin.Context) {
	var articleUserList []model.ArticleUserList
	/*
		SELECT
		  article12.id ArticleId,
		  user12.user_name AS UserName,
		  user12.`password` PASSWORD,
		  article12.title Title,
		  article12.author Author,
		  article12.content Content
		FROM
		  article_users
		  LEFT JOIN article12 ON article12.id = article_users.aid
		  LEFT JOIN user12 ON user12.id = article_users.uid
	*/
	redis_key := "article_user_list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("article_users").
			Select(
				" article12.id ArticleId",
				"user12.user_name AS UserName",
				"user12.`password` Password",
				"article12.title Title",
				"article12.author Author",
				"article12.content Content",
			).
			Joins("LEFT JOIN article12 ON article12.id = article_users.aid").
			Joins("LEFT JOIN user12 ON user12.id = article_users.uid").
			Debug().Find(&articleUserList)
		jsonData, _ := json.Marshal(articleUserList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &articleUserList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": articleUserList,
	})
	return
}

func ArticleUser(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  article12
		  LEFT JOIN article_users ON article12.id = article_users.aid
		WHERE
		  article_users.uid = 1
	*/
	var article model.Article12
	redis_key := "article_user_" + uid
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("article12").
			Joins("LEFT JOIN article_users ON article12.id = article_users.aid").
			Where("article_users.uid =?", uid).Debug().Find(&article)
		jsonData, _ := json.Marshal(&article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &article)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": article,
	})
	return
}

func Info(c *gin.Context) {
	uid, _ := c.Get("uid")
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"uid":  uid,
	})
	return
}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "user_login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次,已封禁两小时",
		})
		return
	}
	var user model.User12
	if err := model.DB.Where("user_name=?", username).First(&user).Debug().Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次,封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误请重新输入",
		})
		return
	}
	token := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	})
	return
}
